﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;

namespace PPI.Controls.Grid
{
    public partial class GridDistributionMeters : GridBase
    {
        private List<EMeter> m_Meters;

        public List<EMeter> CurrentMeters
        {
            get
            {
                return m_Meters;
            }
        }
        
        public EDistributionPole Pole {get; set;}

        public GridDistributionMeters()
        {
            InitializeComponent();
            cmiEditWhithOutImages.Visible = false;
        }

        public void LoadMeters(string connectionString)
        {
            m_Meters = LMeter.GetPoleMeters(Pole, connectionString);
            
            LoadMeters(m_Meters);
        }

        public void LoadMeters(List<EMeter> meters)
        {
            m_Meters = meters;
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            DataTable dt = new DataTable("Inspection");

            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));

            foreach (EMeter meter in m_Meters)
                dt.Rows.Add(meter.Id, meter.Latitude == double.MinValue ? 0 : meter.Latitude, meter.Longitude == double.MinValue ? 0 : meter.Longitude);

            datagridBase.AutoGenerateColumns = true;
            datagridBase.DataSource = dt;
            datagridBase.Refresh();
        }
    }
}
